package androidvn.xenforo.fourums.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import androidvn.xenforo.fourums.AuthenticationFragment;
import androidvn.xenforo.fourums.R;
import androidvn.xenforo.fourums.model.NavDrawerItem;
import android.app.FragmentManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpListAdapterLeft extends BaseExpandableListAdapter {

	private Context context;
	private ArrayList<NavDrawerItem> menuList;
	private HashMap<NavDrawerItem, ArrayList<NavDrawerItem>> menuChildList;
	private FragmentManager manager;

	public ExpListAdapterLeft(Context context,
			ArrayList<NavDrawerItem> menuList,
			HashMap<NavDrawerItem, ArrayList<NavDrawerItem>> menuChildList,
			FragmentManager manager) {
		super();
		this.context = context;
		this.menuList = menuList;
		this.menuChildList = menuChildList;
		this.manager = manager;
	}

	@Override
	public NavDrawerItem getChild(int groupPosition, int childPosition) {
		return menuChildList.get(menuList.get(groupPosition))
				.get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		String childTitle = getChild(groupPosition, childPosition).getTitle();
		int childIcon = getChild(groupPosition, childPosition).getIcon();
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.left_menu_list_child_item,
					null);
		}

		ImageView icon = (ImageView) convertView.findViewById(R.id.ivIcon);
		TextView title = (TextView) convertView.findViewById(R.id.tvTitle);
		title.setText(childTitle);
		icon.setImageResource(childIcon);
		icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return menuChildList.get(menuList.get(groupPosition)).size();
	}

	@Override
	public NavDrawerItem getGroup(int groupPosition) {
		return menuList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return menuList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup viewGroup) {
		ViewHolder holder = null;
		String parentTitle = getGroup(groupPosition).getTitle();

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.left_menu_list_item,
					viewGroup, false);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.tvTitle);
			holder.icon = (ImageView) convertView.findViewById(R.id.ivButton);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.title.setText(parentTitle);
		// displaying icon
		// check whether it set visible or not
		if (menuList.get(groupPosition).isCounterVisible()) {
			holder.icon.setImageResource(menuList.get(groupPosition).getIcon());

		} else {
			// hide the icon view
			holder.icon.setVisibility(View.GONE);
		}

		holder.icon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (groupPosition == 2) {
					AuthenticationFragment authen = new AuthenticationFragment();
					authen.show(manager, "authen");
				} else if (groupPosition == 4) {
					AuthenticationFragment authen = new AuthenticationFragment();
					authen.show(manager, "authen");
				}
			}
		});

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	static class ViewHolder {
		TextView title;
		ImageView icon;
	}
}
